/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FangYongliang
 * 创建时间:    2023-10-13
 */
package org.jeecg.modules.dock.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.controller.EmsDockingController;
import org.jeecg.modules.dock.entity.DockTaskRecordMap;
import org.jeecg.modules.dock.entity.mes.request.MesBaseRequestVO;
import org.jeecg.modules.dock.entity.mes.request.VerticalAndHorizontalCuttingRequestVO;
import org.jeecg.modules.dock.entity.mes.request.VerticalCuttingIsCalledPaperTubeRequestVO;
import org.jeecg.modules.dock.enums.EmsBucketStatusEnum;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.mapper.DockTaskRecordMapMapper;
import org.jeecg.modules.dock.service.IDockTaskRecordMapService;
import org.jeecg.modules.dock.service.MesDockingRepeaterService;
import org.jeecg.modules.flow.sys.util.SpringContextUtil;

import java.util.ArrayList;
import java.util.Date;

/**
 * EMS推送状态更新
 *
 * @author FangYongliang
 * @version 1.0.0
 * @since 2023-10-13
 */
public class EmsBucketStatusHandler implements StatusHandler<EmsDockingController.TaskTypeUpdateParam> {


    private DockTaskRecordMapMapper dockTaskRecordMapMapper;
    private IWmsMoveReceiptMaterialService iWmsMoveReceiptMaterialService;
    private IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;
    private IDockTaskRecordMapService iDockTaskRecordMapService;


    private ArrayList<String> abnormalStatus = Lists.newArrayList(EmsBucketStatusEnum.CANCEL_TASK.getCode(),
            EmsBucketStatusEnum.PAUSE_TASK.getCode(), EmsBucketStatusEnum.FORCE_FINISHED.getCode());

    public EmsBucketStatusHandler() {
        this.dockTaskRecordMapMapper = SpringContextUtil.getBean(DockTaskRecordMapMapper.class);
        this.iWmsMoveReceiptMaterialService = SpringContextUtil.getBean(IWmsMoveReceiptMaterialService.class);
        this.iDockTaskRecordMapService = SpringContextUtil.getBean(IDockTaskRecordMapService.class);
        this.iWmsInReceiptTaskMaterialService = SpringContextUtil.getBean(IWmsInReceiptTaskMaterialService.class);
    }

    @Override
    public void handle(EmsDockingController.TaskTypeUpdateParam param) {

        DockTaskRecordMap dockTaskRecordMap = dockTaskRecordMapMapper.getByTaskCode(param.getTaskCode());
        if (dockTaskRecordMap == null){
            throw new JeecgBootException("未通过taskCdoe找到任务");
        }
        MesDockingRepeaterService service = SpringContextUtil.getBean(MesDockingRepeaterService.class);
        //叫纸筒
        if (SystemConstants.TaskType.ZJ_TAKE_PAPER.equals(param.getTaskType())) {
            VerticalCuttingIsCalledPaperTubeRequestVO vo = new VerticalCuttingIsCalledPaperTubeRequestVO();
            // 根据MES任务编号查询任务记录
            if (ObjectUtil.isNotNull(dockTaskRecordMap)) {
                WmsMoveReceiptMaterial wmsMoveReceiptMaterial = iWmsMoveReceiptMaterialService.list(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                        .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, dockTaskRecordMap.getTaskId())).get(0);
                if (ObjectUtil.isNotNull(wmsMoveReceiptMaterial)) {
                    vo = JSON.parseObject(wmsMoveReceiptMaterial.getExtraText(), VerticalCuttingIsCalledPaperTubeRequestVO.class);
                }
            }
            param.setSite(vo.getSite());
            param.setOperateTime(new Date());
            if (EmsBucketStatusEnum.TASK_RECEIVED.getCode().equals(param.getTaskStatus())) {

            } else if (EmsBucketStatusEnum.TASK_STARTED.getCode().equals(param.getTaskStatus())) {
                param.setTaskStatus(EmsBucketStatusEnum.TASK_STARTED.getCode());
                service.updateStatus(getRequest(param));
            } else if (EmsBucketStatusEnum.SEND_BUCKET_FINISHED.getCode().equals(param.getTaskStatus())) {
                // 更新流程状态
                workFlowService.backAutoCommit(dockTaskRecordMap.getTaskId(), null);
                param.setTaskStatus(EmsBucketStatusEnum.SEND_BUCKET_FINISHED.getCode());
                service.updateStatus(getRequest(param));

            } else if (EmsBucketStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                pauseFlow(dockTaskRecordMap.getTaskId());

            } else if (EmsBucketStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                cancelFlow(dockTaskRecordMap.getTaskId());
            } else if (EmsBucketStatusEnum.TAKE_BUCKET_FINISHED.getCode().equals(param.getTaskStatus())) {
                // 更新流程状态
                workFlowService.backAutoCommit(dockTaskRecordMap.getTaskId(), null);
                param.setTaskStatus(EmsBucketStatusEnum.SEND_BUCKET_FINISHED.getCode());
                service.updateStatus(getRequest(param));
            }

            //退纸筒
        } else if (SystemConstants.TaskType.ZJ_RETURN_PAPER.equals(param.getTaskType())) {
            VerticalAndHorizontalCuttingRequestVO vo = new VerticalAndHorizontalCuttingRequestVO();
            // 根据MES任务编号查询任务记录
            if (ObjectUtil.isNotNull(dockTaskRecordMap)) {
                WmsInReceiptTaskMaterial wmsInReceiptTaskMaterial = iWmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                        .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, dockTaskRecordMap.getTaskId())).get(0);
                if (ObjectUtil.isNotNull(wmsInReceiptTaskMaterial)) {
                    vo = JSON.parseObject(wmsInReceiptTaskMaterial.getExtraText(), VerticalAndHorizontalCuttingRequestVO.class);
                }
            }
            param.setSite(vo.getSite());
            param.setOperateTime(new Date());
            if (EmsBucketStatusEnum.TASK_STARTED.getCode().equals(param.getTaskStatus())) {
                param.setTaskStatus(EmsBucketStatusEnum.TASK_STARTED.getCode());
                service.updateStatus(getRequest(param));
            } else if (EmsBucketStatusEnum.RETURN_BUCKET_FINISHED.getCode().equals(param.getTaskStatus())) {
                param.setTaskStatus(EmsBucketStatusEnum.RETURN_BUCKET_FINISHED.getCode());
                workFlowService.backAutoCommit(dockTaskRecordMap.getTaskId(), null);
                service.updateStatus(getRequest(param));
            } else if (EmsBucketStatusEnum.TAKE_BUCKET_FINISHED.getCode().equals(param.getTaskStatus())) {
                param.setTaskStatus(EmsBucketStatusEnum.TAKE_BUCKET_FINISHED.getCode());
                workFlowService.backAutoCommit(dockTaskRecordMap.getTaskId(), null);
                service.updateStatus(getRequest(param));
            } else if (abnormalStatus.contains(param.getTaskStatus())) {
                //异常状态
            } else if (EmsBucketStatusEnum.PAUSE_TASK.getCode().equals(vo.getTaskStatus())) {
                pauseFlow(dockTaskRecordMap.getTaskId());

            } else if (EmsBucketStatusEnum.CANCEL_TASK.getCode().equals(vo.getTaskStatus())) {
                cancelFlow(dockTaskRecordMap.getTaskId());
            }
        }

    }

    private MesBaseRequestVO getRequest(EmsDockingController.TaskTypeUpdateParam param) {
        return getRequest(param, param.getTaskStatus());
    }

    private MesBaseRequestVO getRequest(EmsDockingController.TaskTypeUpdateParam param, String taskStatus) {
        MesBaseRequestVO requestVO = new MesBaseRequestVO();
        requestVO.setTaskCode(param.getTaskCode());
        requestVO.setTaskStatus(taskStatus);
        requestVO.setSite(param.getSite());
        requestVO.setOperateTime(new Date());
        requestVO.setTaskType(param.getTaskType());
        requestVO.setLineName(param.getLineName());
        requestVO.setOperateReason(param.getOperateReason());
        requestVO.setRemark(param.getRemark());
        requestVO.setOperatorName(param.getOperatorName());
        return requestVO;
    }
}
